/*******************************************************************************
 * Copyright (c) Jonathan Buck - jonbuck16@gmail.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    jonbuck16@gmail.com - initial API and implementation
 *******************************************************************************/
package net.jonbuck.tassoo.ui.wizard;

import net.jonbuck.tassoo.model.Container;
import net.jonbuck.tassoo.model.TassooFactory;
import net.jonbuck.tassoo.model.TassooPackage;
import net.jonbuck.tassoo.persistence.dao.TassooDao;
import net.jonbuck.tassoo.persistence.dao.TassooDaoImpl;
import net.jonbuck.tassoo.persistence.exception.TassooCreationException;
import net.jonbuck.tassoo.ui.wizard.model.NewContainerModel;
import net.jonbuck.tassoo.ui.wizard.page.ContainerDetailsPage;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

/**
 * 
 * 
 * @since 1.0.0
 */
public class NewContainerWizard extends Wizard implements INewWizard {

	private NewContainerModel newContainerModel;
	private TassooPackage tassooPackage;
	private TassooFactory tassooFactory;
	
	/** Tassoo Data Access Object. */
	private TassooDao tassooDao = TassooDaoImpl.getInstance();
	
	/**
	 * 
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		newContainerModel = new NewContainerModel();
		tassooPackage = TassooPackage.eINSTANCE;
		tassooFactory = tassooPackage.getTassooFactory();
	}

	/**
	 * 
	 */
	public void addPages() {
		addPage(new ContainerDetailsPage(ContainerDetailsPage.PAGE_NAME, newContainerModel));
	}

	/**
	 * 
	 */
	public boolean performFinish() {
		try {
			Container container = tassooFactory.createContainer();
			container.setContainerName(newContainerModel.getContainerName());
			container.setContainerDescription(newContainerModel.getContainerDescription());
			tassooDao.createNewContainer(container);
			
		} catch (TassooCreationException tce) {
			// TODO Handle Exception here...
			System.out.println(tce.getMessage());
			return false;
		}
		return true;
	}

}
